import request from '@common/js/api.js'
// import lyMenuList from '@/router/pcModules/leftNav.js'
import { Message } from 'element-ui'
import { _setLocalItem, _getLocalItem, _sortBy } from '@/common/js/tool.js'
// 路由权限管理
import { asyncRouter } from '@/router/asyncRoutes.js'
import leftNav from '@/router/leftNav.js'

const storeToken = _getLocalItem('jx_access_token') || ''
const lyMenuList = _getLocalItem('lyMenuList') || []
const user = {
  state: {
    isCustomer: _getLocalItem('isCustomerLogin') || false, // 是否客户登录
    lyMenuList: lyMenuList, // 左侧菜单树渲染
    token: storeToken || '',
    userinfo: ''
  },
  getters: {
    isCustomerLogin: function(state) {
      // 是否客户登录
      return state.isCustomer
    },
    lyMenuList: function(state) {
      return state.lyMenuList
    }
  },
  mutations: {
    SET_ISCUSTOMER: (state, view) => {
      state.isCustomer = view
    },
    SET_TOKEN: (state, view) => {
      state.token = view
    },
    SET_MENULIST: (state, view) => {
      state.lyMenuList = view
      _setLocalItem('lyMenuList', view)
    }
  },
  actions: {
    // // 获取权限信息
    jx_ReqUserMsg({ commit }) {
      return new Promise((resolve, reject) => {
        reqLoginInfo()
          .then(res => {
            if (res.code == 0) {
              //   commit('SET_USER_MSG', res.data)
              resolve({ res: res.data })
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    },
    // 获取权限菜单
    jx_ReqUserMenu({ commit }) {
      return new Promise((resolve, reject) => {
        reqMenu()
          .then(res => {
            // console.log(res)
            if (res.code == 0) {
              const data = res.data.responseBody
              _setLocalItem('jx_allLYPcMenuAndPermissions', data)
              let myMenu = []
              // 过滤菜单 lypc开头
              if (data.length > 0) {
                myMenu = data.filter(v => v.perms.indexOf('lypc_') > -1)
                myMenu = myMenu.sort(_sortBy('sort'))
                const finalMenu = getRoutes(getLeftNavFinal(leftNav), getPath(myMenu))
                const tmpArr = asyncRouter[0]
                tmpArr.children = finalMenu
                // commit('SET_USER_MENU', [tmpArr])
                commit('SET_MENULIST', finalMenu)
              }
              resolve({ code: 0, data: myMenu })
            } else {
              resolve({ code: 1 })
            }
          })
          .catch(error => {
            reject(error)
          })
      })
    }
  }
}
// 获取权限信息
const reqLoginInfo = param => {
  const token = _getLocalItem('jx_access_token') ? _getLocalItem('jx_access_token') : ''
  return new Promise((resolve, reject) => {
    request.get('/ly-code-auth-servicer/oauth/check_token', { token: token }, res => {
      resolve({ code: 0, data: res })
    })
    // resolve({ code: 0 })
  })
}
const reqMenu = param => {
  return new Promise((resolve, reject) => {
    request.get('/ly-code-admin-servicer/menu/user?clientId=oms', '', res => {
      if (res.code == 0 && !res.responseBody) {
        Message({
          type: 'error',
          message: '当前用户暂无权限'
        })
      } else {
        resolve({ code: 0, data: res })
      }
    })
  })
}
// 获取合并后的菜单列表
function getRoutes(arr, cArr) {
  // console.log(arr, cArr)
  return arr.filter(v => {
    if (v.path == '/homePage') {
      // 首页固定
      return true
    }
    if (v.children && v.children.length > 0) {
      v.children = getRoutes(v.children, cArr)
    }
    if (v.finalPath && cArr.indexOf(v.finalPath) > -1) {
      return true
    }
    return false
  })
}
// 获取请求返回的所有的path
function getPath(data, arr = []) {
  data.forEach(v => {
    if (v.path) {
      arr.push(v.path)
    }
    if (v.children && v.children.length > 0) {
      arr.concat(getPath(v.children, arr))
    }
  })
  return arr
}
// 获取leftNav的所有path
function getLeftNavFinal(nav, base = '') {
  nav.forEach(v => {
    v.finalPath = base + v.path
    if (v.children && v.children.length > 0) {
      v.children = getLeftNavFinal(v.children, v.finalPath + '/')
    }
  })
  return nav
}
// 获取当前系统的所有权限
// function getPermis(arr) {
//   return arr && arr.length > 0 ? arr.filter(v => v.indexOf('omspc_') > -1) : []
// }
export default user
